package com.cxs.componentcode.slice;

import com.cxs.componentcode.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class TabListSlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");
    private TabList tabList = null;
    private ScrollView scrollView = null;
    // TabContent的id列表
    int[] pageId = new int[]{
            ResourceTable.Layout_tab_list_1, ResourceTable.Layout_tab_list_2, ResourceTable.Layout_tab_list_3
    };
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_tab_list);

        scrollView = (ScrollView) findComponentById(ResourceTable.Id_tab_content);
        tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);

        tabList.addTab(createTab("图片"));
        tabList.addTab(createTab("视频"));
        tabList.addTab(createTab("音频"));
        tabList.setFixedMode(true);

        DirectionalLayout page = (DirectionalLayout)LayoutScatter.getInstance(getContext())
                .parse(pageId[0], null, false);
        scrollView.addComponent(page);
        tabList.selectTabAt(0);

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                int position = tab.getPosition();
                DirectionalLayout page = (DirectionalLayout)LayoutScatter.getInstance(getContext())
                        .parse(pageId[position], null, false);
                try {
                    scrollView.addComponent(page);
                } catch (Exception e) {
                    HiLog.info(LABEL, position+ " " +e.getMessage());
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
                scrollView.removeAllComponents();
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
            }
        });
    }
    // 设置Tab的属性
    private TabList.Tab createTab(String tabName) {
        TabList.Tab tab = tabList.new Tab(this);
        tab.setText(tabName);
        tab.setTextSize(14, Text.TextSizeType.FP);
        tab.setPadding(12, 0, 12, 0);
        return tab;
    }

}
